﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WareHouse.App
{
    using WareHouse.Contract.Models;
    using WareHouse.Contract.Services;
    using WareHouse.Service.Services;

    public partial class FrmWareHouse : Form
    {
        private WareHouseModel model = new WareHouseModel();

        private IWareHouseService wareHouseService;
        public FrmWareHouse()
        {
            wareHouseService=new WareHouseService();
            InitializeComponent();
        }


        private void UpdateModel()
        {
            model.WareHouseId = int.Parse(txtWareHouseId.Text != "" ? txtWareHouseId.Text : "0");
            model.WareHouseName = txtWareHouseName.Text;
            model.WareHouseDescription = txtWareHouseDescription.Text;
            model.WareHouseTypeId = int.Parse(ddlWareHouseType.SelectedValue.ToString());
            if (chkIsSale.Checked)
            {
                model.IsSale = true;
            }
            else
            {
                model.IsSale = false;
            }
         
            model.UpdateBy = General.userName;
            model.LastUpdate = DateTime.Now;

        }


        private void LoadDataGrid()
        {
            this.dtgWareHouse.DataSource = this.wareHouseService.ListWareHouse();
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {





            if (btnAdd.Text == "THÊM MỚI")
            {
                General.ChangeButtonStatus("insert", btnAdd, btnUpdate, btnDelete);

                txtWareHouseId.Text = "";
                txtWareHouseName.Text = "";
                txtDateCreate.Text = "";

                txtLastUpdate.Text = "";
                txtUserCreate.Text = "";
                txtUserUpdate.Text = "";

                return;
            }
            if (btnAdd.Text == "LƯU")
            {


                if (!Validator.IsNull(txtWareHouseName.Text))
                {
                    MessageBox.Show("Tên kho không thể bỏ trống");
                    return;
                }
                if (ddlWareHouseType.Text =="")
                {
                    MessageBox.Show("Chọn loại kho");
                    return;
                }
                UpdateModel();
                model.EffectiveFromDate = DateTime.Now;
                model.DateCreate = DateTime.Now;
                model.WareHouseId = 0;
                model.UserCreate = General.userName;
                var result = this.wareHouseService.SaveOrUpdateWareHouse(this.model);
                if (result != null)
                {

                    MessageBox.Show("Thêm mới thành công", "Thông báo");

                    General.ChangeButtonStatus("insert", this.btnAdd, this.btnUpdate, this.btnDelete);
                    this.LoadDataGrid();
                }
            }
            else
                MessageBox.Show("Thêm mới thất bại vui lòng kiểm tra lại dữ liệu ", "Thông báo");


        }

        private void btnDelete_Click(object sender, EventArgs e)
        {


            if (btnDelete.Text == "HỦY")
            {
                General.ChangeButtonStatus("update", btnAdd, btnUpdate, btnDelete);

            }
            else if (btnDelete.Text == "XÓA")
            {
                if (MessageBox.Show(General.delConfirmInfo, General.delConfirmCaption, MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == DialogResult.Yes)
                {
                    this.UpdateModel();
                    if (model.WareHouseId > 0)
                    {
                        var result = wareHouseService.DeleteWareHouse(model);
                        if (result)
                        {
                            General.ChangeButtonStatus("delete", btnAdd, btnUpdate, btnDelete);
                            MessageBox.Show("Xóa thành công", "Thông báo");
                            this.LoadDataGrid();
                            return;
                        }
                        MessageBox.Show("Xóa thất bại", "Thông báo");

                    }
                }
            }
            else return;
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {

            if (btnUpdate.Text == "CẬP NHẬT")
            {
                General.ChangeButtonStatus("update", btnAdd, btnUpdate, btnDelete);

                return;

            }
            else
                if (btnUpdate.Text == "HỦY")
                {
                    General.ChangeButtonStatus("insert", btnAdd, btnUpdate, btnDelete);


                }
                else if (btnUpdate.Text == "LƯU")
                {
                    if (model.WareHouseId == 0)
                    {
                        MessageBox.Show("Chọn kho cần cập nhật");
                        return;
                    }
                    if (!Validator.IsNull(txtWareHouseName.Text))
                    {
                        MessageBox.Show("Tên kho không thể bỏ trống");
                        return;
                    }

                    if (ddlWareHouseType.Text == "")
                    {
                        MessageBox.Show("Chọn loại kho");
                        return;
                    }

                    UpdateModel();
                    var result = wareHouseService.SaveOrUpdateWareHouse(model);


                    if (result != null)
                    {
                        MessageBox.Show("Cập nhật thành công", "Thông báo");
                        this.LoadDataGrid();
                        General.ChangeButtonStatus("update", btnAdd, btnUpdate, btnDelete);

                    }
                }
                else
                    MessageBox.Show("Cập nhật thất bại vui lòng kiểm tra lại dữ liệu ", "Thông báo");
        }
        private void FrmWareHouse_Load(object sender, EventArgs e)
        {
            CheckRole();
            this.LoadDataGrid();
            LoadWareHouseType();
        }
        private void CheckRole()
        {
            if (!wareHouseService.R(General.groupUserId, "warehouse_add"))
            {
                btnAdd.Enabled = false;

            }
            if (!wareHouseService.R(General.groupUserId, "warehouse_edit"))
            {
                btnUpdate.Enabled = false;

            }
            if (!wareHouseService.R(General.groupUserId, "warehouse_delete"))
            {
                btnDelete.Enabled = false;

            }

        }

        private void LoadWareHouseType()
        {
            ddlWareHouseType.DataSource = wareHouseService.ListWareHouseType();
            ddlWareHouseType.ValueMember = "WareHouseTypeId";
            ddlWareHouseType.DisplayMember = "WareHouseTypeName";
        }

        private void dtgWareHouse_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                model = (WareHouseModel)dtgWareHouse.SelectedRows[0].DataBoundItem;


                txtWareHouseId.Text = model.WareHouseId.ToString();
                txtWareHouseName.Text = model.WareHouseName;
                txtWareHouseDescription.Text = model.WareHouseDescription;
                if (model.IsSale)
                {
                    chkIsSale.Checked = true;
                }
                else
                {
                    chkIsSale.Checked = false;
                }

                ddlWareHouseType.SelectedValue = model.WareHouseTypeId;
                txtDateCreate.Text = model.DateCreate != null
                                         ? string.Format(
                                             "{0:dd-MM-yyyy}",
                                             model.LastUpdate)
                                         : "";

                txtLastUpdate.Text = model.LastUpdate != null ? string.Format(
                                             "{0:dd-MM-yyyy}",
                                            model.LastUpdate) :
                "";
                txtUserCreate.Text = this.model.UserCreate ?? "";
                txtUserUpdate.Text = this.model.UpdateBy ?? "";
            }
            catch (Exception)
            {

                return;
            }
        }
    }
}
